package com.fsnbigdata.repository;
import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.AnimalConservation;
import com.fsnbigdata.domain.QAnimalConservation;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

/**
 * = AnimalConservationRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = AnimalConservationRepositoryCustom.class)
@Transactional(readOnly = true)
public class AnimalConservationRepositoryImpl extends QueryDslRepositorySupportExt<AnimalConservation> implements AnimalConservationRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    AnimalConservationRepositoryImpl() {
        super(AnimalConservation.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM_ID = "organismId";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATOR = "creator";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String LATIN = "latin";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    @Override
    public Page<AnimalConservation> findAll(Predicate predicate, Pageable pageable) {
        QAnimalConservation animalConservation = QAnimalConservation.animalConservation;
        JPQLQuery<AnimalConservation> query = from(animalConservation).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        return loadPage(query, pageable, animalConservation);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<AnimalConservation> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        QAnimalConservation animalConservation = QAnimalConservation.animalConservation;
        JPQLQuery<AnimalConservation> query = from(animalConservation);
        if(creator != null ){
            query.where(animalConservation.creator.eq(creator));
        }
        Path<?>[] paths = new Path<?>[] { animalConservation.name, animalConservation.latin, animalConservation.organismId, animalConservation.creator };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(NAME, animalConservation.name).map(LATIN, animalConservation.latin).map(ORGANISM_ID, animalConservation.organismId).map(CREATOR, animalConservation.creator);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, animalConservation);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<AnimalConservation> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        QAnimalConservation animalConservation = QAnimalConservation.animalConservation;
        JPQLQuery<AnimalConservation> query = from(animalConservation);
        Path<?>[] paths = new Path<?>[] { animalConservation.name, animalConservation.latin, animalConservation.organismId, animalConservation.creator };
        applyGlobalSearch(globalSearch, query, paths);
        // Also, filter by the provided ids
        query.where(animalConservation.id.in(ids));
        AttributeMappingBuilder mapping = buildMapper().map(NAME, animalConservation.name).map(LATIN, animalConservation.latin).map(ORGANISM_ID, animalConservation.organismId).map(CREATOR, animalConservation.creator);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, animalConservation);
    }
}
